/* vim: set ts=2 et sw=2 cindent fo=qroca: */

package com.globant.google.mendoza;

import java.util.HashMap;
import java.util.Map;

import com.globant.google.mendoza.malbec.schema._2.NewOrderNotification;
import com.globant.google.mendoza.malbec.schema._2.RiskInformationNotification;

/** Keeps the received messages from the Checkout server.
 */
public final class TransportProxyMessages {

  /** The new order notification index. */
  private final int non = 0;

  /** The risk information notification index. */
  private final int risk = 1;

  /** The financial order state index. */
  private final int fin = 2;

  /** The fulfillment order state index. */
  private final int ful = 3;

  /** The notifications array size. */
  private final int arraySize = 4;

  /** The received messages. */
  private Map<String, Object[]> receivedMessages =
    new HashMap<String, Object[]>();

  /** Adds a new order notification message.
   *
   * @param orderNumber The google order number.
   *
   * @param newOrderNotification The new order notification.
   */
  public void addNewOrderNotification(final String orderNumber,
      final NewOrderNotification newOrderNotification) {
    add(non, orderNumber, newOrderNotification);
  }

  /** Adds a risk information notification.
   *
   * @param orderNumber The google order number.
   *
   * @param riskNotification The risk notification.
   */
  public void addRiskInformationNotification(final String orderNumber,
      final RiskInformationNotification riskNotification) {
    add(risk, orderNumber, riskNotification);
  }

  /** Adds the order states.
   *
   * @param orderNumber The google order number.
   *
   * @param newFulfillmentState The fulfillment order state.
   *
   * @param newFinancialState The financial order state.
   */
  public void addOrderStateChanged(final String orderNumber,
      final String newFulfillmentState,
      final String newFinancialState) {
    add(fin, orderNumber, newFinancialState);
    add(ful, orderNumber, newFulfillmentState);
  }

  /** Gets the financial order state for the specific order number.
   *
   * @param orderNumber The google order number.
   *
   * @return Returns the financial order state.
   */
  public String getFinancialState(
      final String orderNumber) {
    return (String) get(orderNumber, fin);
  }

  /** Gets the fulfillment order state for the specific order number.
   *
   * @param orderNumber The google order number.
   *
   * @return Returns the fulfillment order state.
   */
  public String getFulfillmentState(
      final String orderNumber) {
    return (String) get(orderNumber, ful);
  }

  /** Gets the new order notification for the specific order number.
   *
   * @param orderNumber The google order number.
   *
   * @return Returns the new order notification
   */
  public NewOrderNotification getNewOrderNotification(
      final String orderNumber) {
    return (NewOrderNotification) get(orderNumber, non);
  }

  /** Gets the risk information notification for the specific order number.
   *
   * @param orderNumber The google order number.
   *
   * @return Returns the risk information notification
   */
  public RiskInformationNotification getRiskInformationNotification(
      final String orderNumber) {
    return (RiskInformationNotification) get(orderNumber, risk);
  }

  /** Gets an element.
   *
   * @param orderNumber The google order number.
   *
   * @param index The index of the element to get.
   *
   * @return The element.
   */
  private Object get(final String orderNumber, final int index) {
    Object result = null;
    if (receivedMessages.containsKey(orderNumber)) {
      Object[] array = receivedMessages.get(orderNumber);
      result = array[index];
    }
    return result;
  }

  /** Adds an element.
   *
   * @param index The index where to add the element.
   *
   * @param key The key.
   *
   * @param o The element to add.
   */
  private void add(final int index, final String key,
      final Object o) {
    if (receivedMessages.containsKey(key)) {
      Object[] array = receivedMessages.get(key);
      array[index] = o;
    } else {
      Object[] array = new Object[arraySize];
      array[index] = o;
      receivedMessages.put(key, array);
    }
  }
}
